package algorithmTopics.string;

public class test8 {

    public static void main(String[] args) {
        test8 test2 = new test8();
        System.out.println(test2.countAndSay(4));
    }public String countAndSay(int n) {
        if (n == 1) {
            return "1";
        }
        // 递归的前一项
        String pre = countAndSay(n - 1);

        String res = "";
        //默认一个
        int j = 1;
        for (int i = 0; i < pre.length(); i++) {
            //当前字符与下一个字符是否相同
            if ( i < pre.length() - 1 && pre.charAt(i) == pre.charAt(i + 1) ) {
                j++;
            } else {
                res += String.valueOf(j * 10 + pre.charAt(i) - '0');
                j = 1;
            }
        }

        return res;
    }

}
